package com.c.ctgproject.ui.collect

import android.content.Intent
import android.view.View
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.c.ctgproject.adapter.guang.PartInWarehouseAdapter
import com.c.ctgproject.base.BaseActivityV2
import com.c.ctgproject.databinding.ActivityAllPartBinding
import com.c.ctgproject.entity.ResultEntity
import com.c.ctgproject.entity.guang.PartInWarehouseEntity
import com.c.ctgproject.event.PositionEvent
import com.c.ctgproject.http.ApiMethods
import com.c.ctgproject.http.MyObserverHandlerLoading
import com.c.ctgproject.utils.*
import com.google.gson.Gson
import org.greenrobot.eventbus.Subscribe
import org.jetbrains.anko.startActivity
import org.jetbrains.anko.startActivityForResult

/**
 *作者:LiBW
 *创建日期: 2021/9/10
 *描述: 散件入库查询 页面
 */
class AllPartActivity : BaseActivityV2(true) , PartInWarehouseAdapter.PartInWarehouseCallBack {
    private val mBinding by lazy { ActivityAllPartBinding.inflate(layoutInflater) }
    override fun getLayout() = mBinding.root
    private var page = 1
    private var search: String = ""
    private val mList = ArrayList<PartInWarehouseEntity.DataBean.ListBean>()
    private val mViewModel:AllPartViewModel by lazy { ViewModelProvider(this)[AllPartViewModel::class.java] }
    private val mAdapter by lazy { PartInWarehouseAdapter(this, mList,mViewModel=mViewModel, listener = this) }

    //    过磅状态 1-已完成 0-未完成
    private var pound: Int? = 0

    @Subscribe
    fun event(type: PositionEvent) {
        if (type.position != -1) {
            mList.removeAt(type.position)
            mAdapter.notifyItemRemoved(type.position)
        }
    }

    override fun event() {
        getData()
        mBinding.mBottomRL.visibility=View.VISIBLE
        mBinding.mRecyclerView.adapter = mAdapter
        mBinding.mRecyclerView.layoutManager = LinearLayoutManager(this)
        mViewModel.searchMine.observe(this, Observer {
            if (it){
                mBinding.mBottomRL.visibility= View.GONE
            }else{
                mBinding.mBottomRL.visibility= View.VISIBLE
            }
        })
        mViewModel.carIdList.observe(this, Observer {
            if(it.isEmpty()){
                mBinding.mConfirmBTN.text="过磅"
            }else{
                mBinding.mConfirmBTN.text="过磅（${it.size}）"
            }
        })
    }

    private var searchMine = false
    override fun click() {
        mBinding.mConfirmBTN.setOnClickListener {
            if(mViewModel.carIdList.value?.size==0){
                showToast("请至少选择一辆车")
            }else{
                startActivityForResult<PartWeightActivity>(
                    1001,
                    MyConstants.ORDER_ID_LIST to Gson().toJson(mViewModel.carIdList.value))
            }
        }
//    //    过磅状态 1-已完成 0-未完成
        mBinding.mTabLayout.handlerSelectReturnText {
            when (it) {
//                "全部" -> {
//                    pound = null
//                    page = 1
//                    getData()
//                }
                "待过磅" -> {
                    searchMine = false
                    mViewModel.changeSearchMine(false)
                    pound = 0
                    page = 1
                    getData()
                }
                "我的" -> {
                    mViewModel.changeSearchMine(true)
                    searchMine = true
                    pound = 1
                    page = 1
                    getData()
                }
            }
        }

        mBinding.mSearchET.handlerText {
            if (it.isEmpty() || it == "") {
                search = ""
                page = 1
                getData()
            }
        }
        mBinding.mSearchTV.setOnClickListener {
            search = mBinding.mSearchET.text.handlerNull()
            page = 1
            getData()
        }
        mBinding.mRefresh.setOnLoadMoreListener {
            page++
            getData()
        }

        mBinding.mRefresh.setOnRefreshListener {
            page = 1
            getData()
        }
    }

    fun getData() {
        mViewModel.clear()//重新获取数据后置空
        if (searchMine) {
//            ApiMethods.apiSubscribe(ApiMethods.retrofit.myPartInputPound(search, page, pound = pound), MyObserverHandlerLoading(this, loading, mBinding.mRefresh) {
            ApiMethods.apiSubscribe(ApiMethods.retrofit.myPartInputPoundV2(2,search, page), MyObserverHandlerLoading(this, loading, mBinding.mRefresh) {
                MyLog.d("我的散件过磅:$it")
                if (page == 1) mList.clear()
                val data = getGson().fromJson(it, PartInWarehouseEntity::class.java).data
                data?.list?.let { list ->
                    mList.addAll(list)
                    mAdapter.notifyDataSetChanged()
                    mBinding.mRecyclerView.handlerPosition(page)
                }
            })

        } else {
            ApiMethods.apiSubscribe(ApiMethods.retrofit.partInWarehouse(search, pound, page), MyObserverHandlerLoading(this, loading, mBinding.mRefresh) {
                MyLog.d("散件入库查询:$it")
                val data = getGson().fromJson(it, PartInWarehouseEntity::class.java).data
                data?.list?.let { list ->
                    if (page == 1) mList.clear()
                    mList.addAll(list)
                    mAdapter.notifyDataSetChanged()
                    mBinding.mRecyclerView.handlerPosition(page)

                }
            })
        }
    }
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        when(requestCode){
            1001->{
                if(resultCode==1){
                    getData()//提交成功后重新获取数据刷新界面
                }
            }
        }
    }

    override fun itemDelete(position: Int, data: PartInWarehouseEntity.DataBean.ListBean) {
//        ApiMethods.apiSubscribe(ApiMethods.retrofit.deleteOut(data.outOrderId?:0), MyObserverHandlerLoading(this, loading, mBinding.mRefresh) {
//            MyLog.d("删除结果:$it")
//            val result= getGson().fromString<ResultEntity<String>>(it)
//            if(result.status==1){
//                mList.removeAt(position)
//                mAdapter.notifyDataSetChanged()
//            }
//        })
    }
}